﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;

namespace Gmantis.Controls
{
    [TemplateVisualState(Name = "Rectangle", GroupName = "ShapeStates"), 
    TemplateVisualState(Name = "Ribbon", GroupName = "ShapeStates"), 
    TemplateVisualState(Name = "Sloped", GroupName = "ShapeStates"), 
    TemplateVisualState(Name = "Rounded", GroupName = "ShapeStates")]
    public class TabItemBorder : ContentControl
    {
        // Fields
        internal bool _isLoaded;
        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(double), typeof(TabItemBorder), new PropertyMetadata(15.0));
        public static readonly DependencyProperty ShapeProperty = DependencyProperty.Register("Shape", typeof(TabItemShape), typeof(TabItemBorder), new PropertyMetadata(TabItemShape.Rectangle, new PropertyChangedCallback(TabItemBorder.OnShapePropertyChanged)));

        // Methods
        public TabItemBorder()
        {
            RoutedEventHandler handler = null;
            base.DefaultStyleKey = typeof(TabItemBorder);
            if (handler == null)
            {
                handler = (param0, param1) => this.ChangeVisualStateShape(false);
            }
            base.Loaded += handler;
        }

        protected void ChangeVisualStateShape(bool useTransitions)
        {
            if (this.Shape == TabItemShape.Sloped)
            {
                VisualStateHelper.GoToState(this, "Sloped", useTransitions);
            }
            if (this.Shape == TabItemShape.Ribbon)
            {
                VisualStateHelper.GoToState(this, "Ribbon", useTransitions);
            }
            if (this.Shape == TabItemShape.Rectangle)
            {
                VisualStateHelper.GoToState(this, "Rectangle", useTransitions);
            }
            if (this.Shape == TabItemShape.Rounded)
            {
                VisualStateHelper.GoToState(this, "Rounded", useTransitions);
            }
        }

        public override void OnApplyTemplate()
        {
            string str = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            if (!string.IsNullOrEmpty(str))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to TabItemBorder.\nDetails: {0}", new object[] { str }));
                }
            }
            else
            {
                this.ChangeVisualStateShape(false);
            }
        }

        private static void OnShapePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as TabItemBorder).ChangeVisualStateShape(true);
        }

        // Properties
        public double CornerRadius
        {
            get { return (double)GetValue(CornerRadiusProperty); }
            set { SetValue(CornerRadiusProperty, value); }
        }

        public TabItemShape Shape
        {
            get { return (TabItemShape)GetValue(ShapeProperty); }
            set { SetValue(ShapeProperty, value); }
        }
    }
}
